package com.jc.oms.controller.yzp;

import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.HttpUtil;
import com.jc.common.util.WebUtils;
import com.jc.service.order.XorderService;
import com.jc.service.yzpService.YZPOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("all")
@RestController
@RequestMapping("yzpOrder")
public class YZPOrderController {

    //分销商品密钥
    private final String appSercret = "c97120ac-3f2d-48e2-b8a7-65acf639d342";

    @Value("${yzp.url}")
    private String url;
    @Autowired
    private XorderService xorderService;
    @Autowired
    private YZPOrderService yzpOrderService;


    @PostMapping("test")
    public String yzpXorder(HttpServletRequest request) {
        String s = "{\"mailNumber\":\"L3256465465907-圆通速递\",\"orderInfo\":{\"201812271899\":{\"荔枝\":1}}}";

        Map<String, Object> map = JSONObject.parseObject(s);
        try {
            String post = HttpUtil.post("http://localhost:8080/oms/yzpOrder/getLogistics", map);
//            String post = HttpUtil.post("http://120.27.224.207:8080/oms/yzpOrder/getLogistics", map);
            return post;
            // map.put("0000", "0000");
        } catch (Exception e) {
            //  map.put("1", "1");
            e.printStackTrace();
        }
        return null;
    }


    @PostMapping("getLogistics")
    public Map<String, Object> getLogistics() {
        Map<String, Object> param = new HashMap<String,Object>();
        Map<String, Object> resultMap = new HashMap<>();
        param.put("appSecret", appSercret);
        if (param == null) {
            resultMap.put("status", "fail");
            resultMap.put("code", "999");
            resultMap.put("code", "参数为空");
            return resultMap;
        }
        try {
            Map<String, Object> res = this.yzpOrderService.getLogistics(param);
            resultMap.put("status", "seccess");
            resultMap.put("code", "200");
            resultMap.put("code", res.get("msg"));
            return resultMap;
        } catch (Exception e) {
            resultMap.put("status", "fail");
            resultMap.put("code", "888");
            resultMap.put("code", "系统异常");
            e.printStackTrace();
            return resultMap;
        }


    }


    /**
     * 更改状态退货中
     */
    @PostMapping("clickTuiHuo")
    @ResponseBody
    public Map<String, Object> clickTuiHuo(HttpServletRequest request) {

        Map<String, Object> params = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = new HashMap<>();


        try {
            params = WebUtils.ReadAsChars(request);
            map = this.yzpOrderService.clickTuiHuo(params);


            if (map != null) {

                result.put("code", "10000");
                result.put("data", map);
            } else {
                result.put("code", "4000");
                result.put("data", "处理失败~~");
            }


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


        return result;
    }


}
